package com.dataStructure.leetcode.树.二叉搜索树;

import com.dataStructure.leetcode.树.TreeNode;

/**
 * https://leetcode-cn.com/problems/invert-binary-tree/
 * 将所有节点的左右子树都交换
 */
public class 翻转一棵二叉树 {
    public TreeNode invertTree(TreeNode root) {
        if (root == null)return root;
        //前序
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;

        invertTree(root.left);
        invertTree(root.right);

        /*//中序
        invertTree(root.left);
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;
        invertTree(root.left);*/

        /*//后序
        invertTree(root.left);
        invertTree(root.right);

        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;*/

        /*//层序
        java.util.Queue<TreeNode>queue = new LinkedList<>();
        queue.offer(root);
        while (!queue.isEmpty()) {
            TreeNode node = queue.poll();

            TreeNode tmp = node.left;
            node.left = node.right;
            node.right = tmp;

            if (node.left != null) {
                queue.offer(node.left);
            }
            if (node.right != null) {
                queue.offer(node.right);
            }
        }*/

        return root;
    }
}
